package com.manlitech.manlichat.service.nettywebsocket;

import com.manlitech.manlichat.base.exception.MyException;
import com.manlitech.manlichat.base.netty.message.container.MessageHandler;
import com.manlitech.manlichat.base.netty.pool.MyChannelHandlerPool;
import com.manlitech.manlichat.base.netty.util.MessageSendUtil;
import com.manlitech.manlichat.dao.ChatMessageDao;
import com.manlitech.manlichat.dao.ImUserDao;
import com.manlitech.manlichat.dao.ImUserFriendDao;
import com.manlitech.manlichat.dto.ImUser;
import com.manlitech.manlichat.dto.ImUserFriend;
import com.manlitech.manlichat.enums.ResultEnums;
import com.manlitech.manlichat.pojo.nettywebsocket.ImUserFriendListMessage;
import com.manlitech.manlichat.pojo.nettywebsocket.ImUserFriendMessage;
import com.manlitech.manlichat.service.ChatMessageService;
import com.manlitech.manlichat.util.ResultUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 获取好友列表
 * @author shensg
 */
@Component
public class ImUserFriendListHandler implements MessageHandler<ImUserFriendListMessage> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    MessageSendUtil messageSendUtil;

    @Autowired
    ChatMessageService chatMessageService;

    @Autowired
    ChatMessageDao chatMessageDao;

    @Autowired
    ImUserDao imUserDao;

    @Autowired
    ImUserFriendDao imUserFriendDao;

    @Autowired
    SQLManager sqlManager;


    @Override
    public void execute(Channel channel, ImUserFriendListMessage message) throws Exception {
        //判断是否绑定
        ChannelId fromChannel = MyChannelHandlerPool.channelIdMap.get(message.getFromId());
        if(fromChannel != null){
            ImUser imUserFrom = imUserDao.createLambdaQuery().andEq("uniquekey",message.getFromId()).single();
            List<ImUser> friends = null;
            if(imUserFrom != null){
                SQLReady sqlReady = new SQLReady(
                        "select iu.* from  " +
                                "( " +
                                "SELECT im_user_id_to as userid FROM `im_user_friend` where im_user_id_from = ?  and status = 1 " +
                                "union  " +
                                "SELECT im_user_id_from as userid FROM `im_user_friend` where im_user_id_to = ? and status = 1  ) t  " +
                                "left join im_user iu on t.userid =   iu.id;" , imUserFrom.getId(),imUserFrom.getId());
                friends = sqlManager.execute(sqlReady,ImUser.class);
            }

            messageSendUtil.responseTheRequest(channel,ResultUtil.success(friends));
            return;
        }
        logger.error(ResultEnums.UN_AUTH.getMsg());
        //删除到channelGroup 通道组
        MyChannelHandlerPool.channelGroup.remove(channel);
        throw new MyException(ResultEnums.UN_AUTH);

    }

    @Override
    public String getType() {
        return ImUserFriendListMessage.TYPE;
    }
}
